<template>
  <div>
    <!-- dialog弹出框 -->
    <el-dialog
      title="更新提交位置"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <!-- from表单 -->
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="unPassInfo" label-width="8rem">
          <el-form-item label="工号">
            {{ unPassInfo.send.sendUserId }}
          </el-form-item>
          <el-form-item label="原提交位置">
            {{ unPassInfo.sendName }}
          </el-form-item>
          <el-form-item label="审核人">
            {{ unPassInfo.actAdminName }}
          </el-form-item>
          <el-form-item label="改变后的位置">
            <el-select v-model="selectRouterName" placeholder="请选择">
              <el-option
                v-for="item in selectRouterAllInfo"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { forUserGetSend, getAddSend, getUpdateSendScore, selectActBySendId, updateSendRouter } from '../../api/send.js'
import { delUnPass } from '../../api/unpass.js'
import { getActInfo, updateActInfoSendStatus } from '../../api/act.js'
export default {
  data() {
    return {
      // 获取全部数据存储位置

      // 不通过信息详情
      unPassInfo: {
        actAdminName: '',
        actId: '',
        actTitle: '',
        send: {},
        sendName: '',
        unpassId: '',
        unpassInfo: {}
      },
      // 页面信息存储位置
      show: false,
      // 选择器信息
      selectRouterAllInfo: [],
      // 选择器选中的信息
      selectRouterName: ''
    }
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // 展示dialog
    showDialog(data, sendAllSelectName, sendAllInfo) {
      // 获取所有党小组信息
      var that = this
      this.selectRouterAllInfo = sendAllSelectName
      this.sendAllInfo = sendAllInfo
      this.show = true
      // 获取不通过信息
      that.unPassInfo = data
      // 存储选择器选中的信息
      that.selectRouterName = data.sendName
    },
    // 提交dialog
    sendDialog() {
      var that = this
      var oldSendName = that.unPassInfo.sendName
      var newSendName = that.selectRouterName
      //   判断选中信息是否和之前信息一致
      if (oldSendName === newSendName) {
        this.$message({
          type: 'warning',
          message: '请修改提交位置后再提交'
        })
      } else {
        that.$confirm('是否确定提交', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 判断提交位置方法
          that.isSendRouterId()
        }).catch(() => {})
      }
    },
    // 判断提交位置方法
    isSendRouterId() {
      var that = this
      const selectRouterInfo = this.sendAllInfo.find((item) => item.key === this.selectRouterName)
      var routerAllId = [selectRouterInfo.value.sendFlistId, selectRouterInfo.value.sendSlistId, selectRouterInfo.value.sendTlistId]
      // 把加上的send分数减掉
      getActInfo(this.unPassInfo.actId).then((res) => {
        const send = {
          sendId: this.unPassInfo.send.sendId,
          sendGiveUserId: sessionStorage.getItem('userId'),
          sendScore: this.unPassInfo.send.sendScore - res.obj.actScore
        }
        // 获取当前活动详细信息
        selectActBySendId(send.sendId).then((sendInfos) => {
          // const actInfo = sendInfos.obj.find((item) => item.actId === that.unPassInfo.actId)
          // 更新image表中的信息（未做）
          // 更新原send表中的信息
          getUpdateSendScore(send, send.sendScore).then((data) => {
          // 更新原始成绩成功
          // 删除unpass表
            that.delUnPassInfo(routerAllId)
          })
        })
      })
    },
    // 删除unpass表
    delUnPassInfo(routerAllId) {
      var that = this
      delUnPass(this.unPassInfo.unpassId).then((res) => {
        // 删除成功
        // 查找当前userId下的send信息（有找到，没有添加一条
        const semId = sessionStorage.getItem('semId')
        const userId = this.unPassInfo.send.sendUserId
        forUserGetSend(semId, userId).then((sendInfos) => {
          const sendInfo = sendInfos.obj.find((item) => item.sendFlistId === routerAllId[0] && item.sendSlistId === routerAllId[1] && item.sendTlistId === routerAllId[2])
          if (sendInfo === undefined) {
            // 添加send表信息
            const send = {
              sendFlistId: routerAllId[0],
              sendSlistId: routerAllId[1],
              sendTlistId: routerAllId[2],
              sendGiveUserId: sessionStorage.getItem('userId'),
              sendSemseterId: sessionStorage.getItem('semId'),
              sendStatus: 0,
              sendUserId: userId
            }
            getAddSend(send).then((data) => {
              // 更新act表信息
              const sendId = data.obj.split(' ')[1]
              that.updateActInfo(sendId)
            })
          } else {
            // 更新send表的信息
            updateSendRouter(sendInfo.sendId, routerAllId[0], routerAllId[1], routerAllId[2], 0, sessionStorage.getItem('userId')).then((res) => {
              // 更新act表信息
              that.updateActInfo(sendInfo.sendId)
            })
          }
        })
      })
    },
    // 修改act表中的信息
    updateActInfo(sendId) {
      updateActInfoSendStatus(this.unPassInfo.actId, sendId).then((res) => {
        // 更新act表中的信息
        this.$message({
          type: 'success',
          message: '修改成功'
        })
        this.show = false
      })
    },
    // 页面逻辑方法
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.$message({
          type: 'success',
          message: '取消审核成功'
        })
      }).catch(() => {})
    }
  }
}
</script>
<style lang="scss">

</style>
